// 节点的创建

class Node{
    constructor(value) {
        this.value = value;
        this.next = null;
    }
}


// 单向链表
class LineLink{
    constructor() {
        this.head = null;
        this.size = 0;
    }
    appendNode(params) {
        let newNode = new Node(params);
        if (!this.head) {
            this.head = newNode;
        } else {
            let currentNode = this.head;
            while(currentNode.next !== null) {
                currentNode = currentNode.next;
            }
            currentNode.next = newNode;
        }
        this.size++
    }
    getSize() {
        return this.size
    }
    getNodeByAt(index) {
        if (index >= 0 && index < this.size) {
            let currentNode = this.head;
            let i = 0;
            while(i < index) {
                currentNode = currentNode.next;
                i++;
            }
            return currentNode
        } else {
            return null
        }
    }
    removeNode(index) {
        if (index >= 0 && index < this.size) {
            if (index > 0) {
                let prevNode = this.getNodeByAt(index - 1);
                let nextNode = this.getNodeByAt(index + 1);
                prevNode.next = nextNode;
            } else if (index === 0) {
                let nowNode = this.head;
                this.head = nowNode.next;
            }
            this.size--;
        } else {
            return null
        }
    }
    insertNode(value, index) {
        if (index >= 0 && index <= this.size) {
            let newNode = new Node(value)
            if (index === 0) {
                let currentNode = this.head;
                this.head = newNode;
                newNode.next = currentNode;
                this.size++;
            } else if (index > 0 && index < this.size) {
                let prevNode = this.getNodeByAt(index - 1);
                let nextNode = this.getNodeByAt(index); 
                prevNode.next = newNode;
                newNode.next = nextNode;
                this.size++;
            } else {
                this.appendNode(value)
            }
        }
    }
}
// [1, 2, 3, 4]

let lineLink = new LineLink();



lineLink.appendNode(1)
lineLink.appendNode(2)
lineLink.appendNode(3)
// lineLink.insertNode(4, 3)
// console.log('lineLink', lineLink)



// 反转链表

function reverseLink(newLineLink) {
    let prev = null;
    let current = newLineLink.head;
    let next = newLineLink.head;
    while(current !== null) {
        [current.next, prev, current] = [prev, current, current.next]
    }
    return prev
}


console.log(reverseLink(lineLink))



// let a = 1;
// let b = 2;
// [a, b] = [b, a]
// console.log(a, b);



class DoubleNode{
    constructor(node) {
        this.prev = null;
        this.value = node;
        this.next = null;
    }
}

class DoubleLink{
    constructor() {
        this.head = null;
        this.size = 0;
    }
    appendNode(params) {
        let newNode = new DoubleNode(params)
        if(!this.head) {
            this.head = newNode;
        } else {
            let currentNode = this.head;
            while(currentNode.next !== null) {
                currentNode = currentNode.next;
            }
            currentNode.next = newNode;
            newNode.prev = currentNode
        }
        this.size++
    }
    getNodeByAt(index) {
        if (index >= 0 && index < this.size) {
            let currentNode = this.head;
            let i = 0;
            while(i < index) {
                currentNode = currentNode.next;
                i++;
            }
            return currentNode
        } else {
            return null
        }
    }
    getSize() {
        return this.size
    }
    insertNode(value, index) {
        if (index >= 0 && index <= this.size) {
            let newNode = new DoubleNode(value)
            if (index === 0) {
                let currentNode = this.head;
                this.head = newNode;
                newNode.next = currentNode;
                currentNode.prev = newNode
                this.size++;
            } else if (index > 0 && index < this.size) {
                let prevNode = this.getNodeByAt(index - 1);
                let nextNode = this.getNodeByAt(index); 
                prevNode.next = newNode;
                newNode.prev = prevNode
                newNode.next = nextNode;
                nextNode.prev = newNode;
                this.size++;
            } else {
                this.appendNode(value)
            }
        }
    }
    removeNode(index) {
        if (index >= 0 && index < this.size) {
            if (index > 0) {
                let prevNode = this.getNodeByAt(index - 1);
                let nextNode = this.getNodeByAt(index + 1);
                prevNode.next = nextNode;
                nextNode.prev = prevNode
            } else if (index === 0) {
                let nowNode = this.head;
                this.head = nowNode.next;
                this.head.prev = null;
            }
            this.size--;
        } else {
            return null
        }
    }
}



let dl = new DoubleLink();



dl.appendNode(1)
dl.appendNode(2)
dl.appendNode(3)
dl.insertNode(4, 0)
dl.insertNode(5, 2)
dl.removeNode(3)
console.log('dl', dl)













